﻿$(document).ready(function() {
    var pollLocation = new google.maps.LatLng($('#latitude').val(), $('#longitude').val());
    var myOptions = {
        zoom: 8,
        center: pollLocation,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    var marker = new google.maps.Marker({
        position: pollLocation,
        map: map,
        title: $('#name').val(),
        draggable: true
    });
    google.maps.event.addListener(marker, 'dragend', function() {
        if (add($('#locationerrormessage')) == false) { return false; }
        var pollID = $('#pollID').val();
        $.ajax({
            type: 'POST',
            url: "/Poll/UpdatePollLocation",
            data: { pollID: $('#pollID').val(), latitude: marker.getPosition().lat(), longitude: marker.getPosition().lng(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
            success: function(response) {
                if (pollID == -1) {
                    $.ajax({
                        type: 'POST',
                        url: "GetTestPoll",
                        data: { pollID: response },
                        success: function(response) {
                            $('#testSAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextTestQuestionNumber').val() + "&pollID=" + response + "&pollName=Test%20Poll");
                        }
                    });
                    $('#SAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=" + $('#name').val());
                    $('#pollID').val(response);
                    $('#main_pollID').val(response);
                    $('#partname').attr("href", "/Poll/Participants?pollID=" + response);
                    $('#list').attr("href", "/Poll/ListofParticipants?pollID=" + response);
                    $('#actionName').val("UpdatePoll");
                }
            }
        });
    });
    $('.colour').each(function() {
        $(this).css("background-color", "#" + $(this).attr("id").substring(6));
        $(this).click(function() {
            $('#defaultFontColour').val("#" + $(this).attr("id").substring(6));
            $('#showFontColour').css("background-color", "#" + $(this).attr("id").substring(6));
            $('#colourpicker').addClass("none");
            $('#updatePoll').val("Save Poll");
            $('#updatePoll').attr("disabled", false);
        });
    });
    $('#showFontColour').click(function() {
        $('#colourpicker').removeClass("none");
    });
    $('#showChartColours').click(function() {
        $('#chartColoursPicker').removeClass("none");
    });
    $('#goyr').click(function() {
        $('#defaultResultsChart_colours').val("green, orange, yellow, red");
        $('#chartColour1').css("background-color", "Green");
        $('#chartColour2').css("background-color", "Orange");
        $('#chartColour3').css("background-color", "Yellow");
        $('#chartColour4').css("background-color", "Red");
        $('#chartColoursPicker').addClass("none");
        $('#updatePoll').val("Save Poll");
        $('#updatePoll').attr("disabled", false);
    });
    $('#rgoy').click(function() {
        $('#defaultResultsChart_colours').val("red, green, orange, yellow");
        $('#chartColour1').css("background-color", "Red");
        $('#chartColour2').css("background-color", "Green");
        $('#chartColour3').css("background-color", "Orange");
        $('#chartColour4').css("background-color", "Yellow");
        $('#chartColoursPicker').addClass("none");
        $('#updatePoll').val("Save Poll");
        $('#updatePoll').attr("disabled", false);
    });
    $('#oygr').click(function() {
        $('#defaultResultsChart_colours').val("orange, yellow, green, red");
        $('#chartColour1').css("background-color", "Orange");
        $('#chartColour2').css("background-color", "Yellow");
        $('#chartColour3').css("background-color", "Green");
        $('#chartColour4').css("background-color", "Red");
        $('#chartColoursPicker').addClass("none");
        $('#updatePoll').val("Save Poll");
        $('#updatePoll').attr("disabled", false);
    });
    $('.datepicker').datetimepicker({ dateFormat: 'dd/mm/yy', ampm: true });
    $('#addCreator').click(function() {
        if ($('#name').val() == "") {
            $('#addCreatorErrorMessage').text("Poll name cannot be blank.");
            $('#name').click(function() {
                $('#addCreatorErrorMessage').text("");
                $('#name').unbind();
            });
            return false;
        }
        if ($('#pollID').val() == "-1") {
            $.ajax({
                type: 'POST',
                url: 'AddPoll',
                data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val() },
                success: function(response) {
                    $('#pollID').val(response);
                    $('#main_pollID').val(response);
                    $.ajax({
                        type: 'POST',
                        url: 'UpdatePollSettings',
                        data: $('#form1').serialize(),
                        success: function() {
                            window.location.href = "/Poll/SelectPollCreator?pollID=" + $('#pollID').val();
                        }
                    });
                }
            });
        }
        else {
            $.ajax({
                type: 'POST',
                url: 'UpdatePoll',
                data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val() },
                success: function() {
                    $.ajax({
                        type: 'POST',
                        url: 'UpdatePollSettings',
                        data: $('#form1').serialize(),
                        success: function() {
                            window.location.href = "/Poll/SelectPollCreator?pollID=" + $('#pollID').val();
                        }
                    });
                }
            });
        }
    });
    $('#addMaster').click(function() {
        if ($('#name').val() == "") {
            $('#addMasterErrorMessage').text("Poll name cannot be blank.");
            $('#name').click(function() {
                $('#addMasterErrorMessage').text("");
                $('#name').unbind();
            });
            return false;
        }
        if ($('#pollID').val() == "-1") {
            $.ajax({
                type: 'POST',
                url: 'AddPoll',
                data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val() },
                success: function(response) {
                    $('#pollID').val(response);
                    $('#main_pollID').val(response);
                    $.ajax({
                        type: 'POST',
                        url: 'UpdatePollSettings',
                        data: $('#form1').serialize(),
                        success: function() {
                            window.location.href = "/Poll/SelectPollMaster?pollID=" + $('#pollID').val();
                        }
                    });
                }
            });
        }
        else {
            $.ajax({
                type: 'POST',
                url: 'UpdatePoll',
                data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val() },
                success: function() {
                    $.ajax({
                        type: 'POST',
                        url: 'UpdatePollSettings',
                        data: $('#form1').serialize(),
                        success: function() {
                            window.location.href = "/Poll/SelectPollMaster?pollID=" + $('#pollID').val();
                        }
                    });
                }
            });
        }
    });
    $('.removeCreator').each(function() {
        var clickedRow = $(this).parent().parent();
        $(this).click(function() {
            $.ajax({
                type: 'POST',
                url: "RemovePollCreator",
                data: { pollID: $('#pollID').val(), UserName: $(this).parent().siblings().first().text() },
                success: function() {
                    clickedRow.remove();
                }
            });
        });
    });
    $('.removeMaster').each(function() {
        var clickedRow = $(this).parent().parent();
        $(this).click(function() {
            $.ajax({
                type: 'POST',
                url: "RemovePollMaster",
                data: { pollID: $('#pollID').val(), UserName: $(this).parent().siblings().first().text() },
                success: function() {
                    clickedRow.remove();
                }
            });
        });
    });
    $('#list').click(function() {
        var pollID = $('#pollID').val();
        if (add($('#listerrormessage')) == false) { return false; }
        if (pollID == -1) {
            $.ajax({
                type: 'POST',
                url: "AddPoll",
                data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
                success: function(response) {
                    window.location.href = "/Poll/ListofParticipants?pollID=" + response;
                }
            });
        }
        else { window.location.href = "/Poll/ListofParticipants?pollID=" + $('#pollID').val(); }
    });
    $('#hasParticipantList').click(function() {
        var checked = $(this).attr('checked');
        if (add($('#parterrormessage')) == false) { return false; }
        $.ajax({
            type: 'POST',
            url: "hasParticipant",
            data: { hasParticipant: checked, pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
            success: function(response) {
                if (checked) {
                    $('#participant').removeClass("none");
                }
                else {
                    $('#participant').addClass("none");
                }
                if ($('#runLink').hasClass("none") && $('#questionCount').val() > 0) {
                    $('#runLink').removeClass("none");
                    $('#testLink').removeClass("none");
                }
                $.ajax({
                    type: 'POST',
                    url: "GetTestPoll",
                    data: { pollID: response },
                    success: function(response) {
                        $('#testSAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextTestQuestionNumber').val() + "&pollID=" + response + "&pollName=Test%20Poll");
                    }
                });
                $('#SAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=" + $('#name').val());
                $('#pollID').val(response);
                $('#main_pollID').val(response);
                $('#partname').attr("href", "/Poll/Participants?pollID=" + response);
                $('#list').attr("href", "/Poll/ListofParticipants?pollID=" + response);
                $('#actionName').val("UpdatePoll");
            }
        });
    });
    $('#updatePoll').click(function() {
        if ($('#name').val() == "") {
            $('#errorMessage').text("Poll name cannot be blank.");
            return false;
        }
        $.ajax({
            type: 'POST',
            url: $('#actionName').val(),
            data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
            success: function(response) {
                $.ajax({
                    type: 'POST',
                    url: "GetTestPoll",
                    data: { pollID: response },
                    success: function(response) {
                        $('#testSAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextTestQuestionNumber').val() + "&pollID=" + response + "&pollName=Test%20Poll");
                    }
                });
                $('#SAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=" + $('#name').val());
                $('#pollID').val(response);
                $('#main_pollID').val(response);
                $('#partname').attr("href", "/Poll/Participants?pollID=" + response);
                $('#list').attr("href", "/Poll/ListofParticipants?pollID=" + response);
                $.ajax({
                    type: 'POST',
                    url: "UpdatePollSettings",
                    data: $('#form1').serialize(),
                    success: function(response) {
                        $('#updatePoll').val("Poll Saved");
                        $('#updatePoll').attr("disabled", true);
                        $('#hierachyLink').removeClass('none');
                        $('#errorMessage').text("");
                        $('#actionName').val("UpdatePoll");
                        var pc = $('#pollClose').val();
                        var hour = pc.slice(11, 13);
                        if (pc.slice(17, 19) == "PM") { hour = parseInt(hour) + 12; }
                        var pollClose = new Date(pc.slice(6, 10), parseInt(pc.slice(3, 5)) -1, pc.slice(0, 2), hour, pc.slice(15, 17));
                        var now = new Date();
                        if ($('#questionCount').val() > 0 && pollClose > now) {
                            $('#runLink').removeClass("none");
                            $('#testLink').removeClass("none");
                        }
                        else {
                            $('#runLink').addClass("none");
                            $('#testLink').addClass("none");
                        }
                    }
                });
            }
        });
        return false;
    });
    $('input[type=text]').each(function() {
        $(this).click(function() {
            $('#updatePoll').val("Save Poll");
            $('#updatePoll').attr("disabled", false);
        });
    });
    $('input[type=checkbox]').each(function() {
        $(this).click(function() {
            $('#updatePoll').val("Save Poll");
            $('#updatePoll').attr("disabled", false);
        });
    });
    $('select').each(function() {
        $(this).click(function() {
            $('#updatePoll').val("Save Poll");
            $('#updatePoll').attr("disabled", false);
        });
    });
    $('#setLocation').click(function() {
        var offset = $(this).offset();
        $('#setPollLocationDialogContainer').css("top", offset.top - 85);
        $('#setPollLocationDialogContainer').css("left", offset.left - 320);
        $('#setLatitude').val("");
        $('#setLongitude').val("");
        $('#setPollLocationErrorMessage').text("");
        $('#setPollLocationDialogContainer').removeClass("none");
    });
    $('#setPollLocationOK').click(function() {
        if (isNaN($('#setLatitude').val()) || isNaN($('#setLatitude').val()) || $('#setLatitude').val() == "" || $('#setLongitude').val() == "") {
            $('#setPollLocationErrorMessage').text("Invalid location");
        }
        else if (add($('#setPollLocationErrorMessage')) != false) {
            var pollID = $('#pollID').val();
            var pollLocation = new google.maps.LatLng($('#setLatitude').val(), $('#setLongitude').val());
            marker.setPosition(pollLocation);
            map.setCenter(pollLocation);
            $.ajax({
                type: 'POST',
                url: "/Poll/UpdatePollLocation",
                data: { pollID: $('#pollID').val(), latitude: $('#setLatitude').val(), longitude: $('#setLongitude').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
                success: function(response) {
                    if (pollID == -1) {
                        $.ajax({
                            type: 'POST',
                            url: "GetTestPoll",
                            data: { pollID: response },
                            success: function(response) {
                                $('#testSAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextTestQuestionNumber').val() + "&pollID=" + response + "&pollName=Test%20Poll");
                            }
                        });
                        $('#actionName').val("UpdatePoll");
                        $('#SAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=" + $('#name').val());
                        $('#pollID').val(response);
                        $('#main_pollID').val(response);
                        $('#partname').attr("href", "/Poll/Participants?pollID=" + response);
                        $('#list').attr("href", "/Poll/ListofParticipants?pollID=" + response);
                    }
                }
            });
            $('#setPollLocationDialogContainer').addClass("none");
        }
    });
    $('#setPollLocationCancel').click(function() {
        $('#setPollLocationDialogContainer').addClass("none");
    });
}); 
function add(error) {
    if ($('#name').val() == "") {
        error.text("Poll name cannot be blank.");
        return false;
    }
    error.text("");
}
function newPoll(error,type) {
    var pollID = $('#pollID').val();
    if (add(error) == false) { return false; }
    if (type == 'real') { var url = "AddPoll"; }
    else {var url = "AddPollGetTest"; }
    if (pollID == -1) {
        $.ajax({
            type: 'POST',
            url: url,
            data: { pollID: $('#pollID').val(), name: $('#name').val(), receiverChannel: $('#receiverChannel').val(), genericUsers: $('#genericUsers').is(' :checked') },
            async: false,
            success: function(response) {
                $('#pollID').val(response);
                if (type == 'real') {
                    $('#SAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=" + $('#name').val());
                    $('#SAQForm').submit();
                }
                else if (type == 'test') {
                    $('#testSAQForm').attr("action", "/Question/EditCustom?questionID=-1&questionNumber=" + $('#nextQuestionNumber').val() + "&pollID=" + response + "&pollName=Test%20Poll");
                    $('#testSAQForm').submit();
                }
            }
        });
        return false;
    }
}